package com.fh.generic;

import java.util.ArrayList;

@SuppressWarnings({"all"})
public class Generic01 {
    public static void main(String[] args) {
        //使用传统的方法来解决
        ArrayList arrayList = new ArrayList();
        arrayList.add(new Dog1("旺财", 10));
        arrayList.add(new Dog1("发财", 1));
        arrayList.add(new Dog1("小黄", 5));
//假如我们的程序员，不小心，添加了一只猫
//        arrayList.add(new Cat("招财猫", 8));
//遍历
        for (Object o : arrayList) {
//向下转型 Object ->Dog
            Dog1 dog = (Dog1) o;
            System.out.println(dog.getName() + "-" + dog.getAge());
        }
    }
}
/*
编写程序，在 ArrayList 中，添加 3 个 Dog 对象
Dog 对象含有 name 和 age, 并输出 name 和 age (要求使用 getXxx())
*/
        class Dog1 {
            private String name;
            private int age;
            public Dog1(String name, int age) {
                this.name = name;
                this.age = age;
            }
            public String getName() {
                return name;
            }
            public void setName(String name) {
                this.name = name;
            }
            public int getAge() {
                return age;
            }
            public void setAge(int age) {
                this.age = age;
            }
        }
        class Cat { //Cat 类
            private String name;
            private int age;
            public Cat(String name, int age) {
                this.name = name;
                this.age = age;
            }
            public String getName() {
                return name;
            }
            public void setName(String name) {
                this.name = name;
            }
            public int getAge() {
                return age;
            }
            public void setAge(int age) {
                this.age = age;
            }
        }
